import os
from utils.data_path_manager import data_path_manager
from utils.logger import logger
from web_test.page.app import App


class DeptPage(App):
    page_function_file = os.path.join(data_path_manager.page_data_dir, "dept", "dept_funcs.yml")

    def __init__(self):
        super().__init__()
        # self.goto_func_page('组织架构', '成员与部门')
        self.driver.get('https://wjvzp7x5tz.feishu.cn/admin/contacts/departmentanduser')
        self.dept_names = []

    def add_dept(self, dept_name, dept_chat, dept_id=None, parent_dept=None, dept_leader=None, dept_chat_leader=None):
        params = {
            "dept_name": dept_name,
            "dept_id": dept_id,
            "parent_dept": parent_dept,
            "dept_leader": dept_leader,
            "dept_chat": dept_chat,
            "dept_chat_leader": dept_chat_leader,
            "alert_key": "alert"
        }
        logger.info(f"add_dept: Params={params}")
        result = self.perform_functions(self.page_function_file, "add_dept", params)
        alert = result.get('alert')
        # logger.info(f"alert: {alert}")
        if alert == '创建成功':
            self.dept_names.append(dept_name)
        elif alert >= '存在' or alert >= "重复":
            logger.info(f"add_dept 创建失败：[{alert}]")
            self.dept_names.append(dept_name)
            self.driver.refresh()
        else:
            logger.info(f"add_dept 创建失败：[{alert}]")
        return result

    def get_dept(self, dept_name=None):
        """按部门名称或用户名称查找"""
        params = {
            "dept_name": dept_name,
            "dept_name_key": "dept_name",
        }
        # logger.info(f"params: {params}")
        self.driver.refresh()
        result = self.perform_functions(self.page_function_file, "get_dept", params)
        return result

    def delete_dept(self, dept_name):
        params = {
            "dept_name": dept_name,
            "alert_key": "alert",
            "deleted_key": "deleted"
        }
        result = self.perform_functions(self.page_function_file, "delete_dept", params)
        # self.driver.refresh()
        return result

    def get_dept_or_user(self, dept_or_user_name=None):
        """按部门名称或用户名称查找"""
        params = {
            "dept_or_user_name": dept_or_user_name,
        }
        logger.info(f"params: {params}")
        self.perform_functions(self.page_function_file, "get_dept", params)
        return self


    def get_selected_deptname_or_username(self):
        """获取部门名称或用户名"""
        params = {
            "name_key": "name",
        }
        info = self.perform_functions(self.page_function_file, "get_selected_deptname_or_username", params)
        # print(info)
        # info["name"] = info["name"]
        logger.info(f"dept_or_user info: {info}")
        return info

    def get_dept_details(self):
        """获取部门名称或用户名"""
        # 元素定位获取到的信保存为dict,key为name
        params = {
            "name_key": "name",
            "id_key": "id",
            "leader_key": "leader",
            "group_name_key": "group_name",
            "group_leader_key": "group_leader",
        }
        info = self.perform_functions(self.page_function_file, "get_dept_details", params)
        # print(info)
        # info["name"] = info["name"]
        logger.info(f"dept_or_user info: {info}")
        return info

    def edit_dept_or_user(self):
        # params = {
        #     "dept_or_user_name": dept_or_user_name,
        # }
        self.perform_functions(self.page_function_file, "edit_dept_or_user")
        return self
